package com.coocaa.launcher.wallpaper;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.coocaa.framework.ui.view.ScrollGridView;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.data.store.com_coocaa_theme.RP_WallpaperCategory;
import com.coocaa.x.uipackage.SkinManager;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.SkyAppPager;
import com.tianci.appstore.utils.SkyConstants;

import java.util.List;

/**
 * Created by luwei on 15-7-4.
 */
public class WallpaperLayout extends SlideFocusView {
    private ScrollTitleBar titleBar = null;
    private SkyAppPager appPager = null;
    private WallpaperController controller = null;
    private ImageView divide = null;
    private TextView tipsView = null;
    private SlideFocusView.FocusViewRevision itemRevision = new SlideFocusView.FocusViewRevision(
            CoocaaApplication.Div(60), CoocaaApplication.Div(65));

    public WallpaperLayout(Context context) {
        super(context, R.drawable.hot_seat_focus_bg);
        setBackgroundResource(R.drawable.launcher_bg);
        addTitleBar();
        addDivideLine();
        addContentView();
        addTipsView();
        setFocusable(false);
        setFocusableInTouchMode(false);
        controller = WallpaperController.getInstance(context);
        controller.setWallpaperLayout(this);
        setFocusVewAlpha(0);
    }

    public ScrollTitleBar getTitleBar() {
        return titleBar;
    }

    public SkyAppPager getAppPager() {
        return appPager;
    }


    private void addContentView() {
        appPager = new SkyAppPager(mContext, SkyAppPager.Direction.VERTICAL);
        appPager.setFocusable(false);
        appPager.setFocusableInTouchMode(false);
        FrameLayout.LayoutParams params = new LayoutParams(
                CoocaaApplication.Div(1740), CoocaaApplication.Div(740), Gravity.CENTER_VERTICAL);
        params.leftMargin = CoocaaApplication.Div(180);
        addView(appPager, params);
        appPager.setTranslationX(CoocaaApplication.Div(380));
    }

    private void addTipsView() {
        tipsView = new TextView(mContext);
        tipsView.setTextColor(Color.WHITE);
        tipsView.setTextSize(CoocaaApplication.Dpi(40));
        tipsView.setGravity(Gravity.CENTER);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        addView(tipsView, params);
    }

    private void addDivideLine() {
        divide = new ImageView(mContext);
        SkinManager.getInstance().setBackgroundResource(divide, R.drawable.launcher_setting_divide);
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                SkyConstants.Div(687), Gravity.CENTER_VERTICAL);
        params.leftMargin = SkyConstants.Div(474);
        addView(divide, params);
    }


    private void addTitleBar() {
        titleBar = new ScrollTitleBar(mContext);
        FrameLayout.LayoutParams params = new LayoutParams(
                CoocaaApplication.Div(477), ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER_VERTICAL);
        addView(titleBar, params);

    }

    public void refreshTitleBar(List<RP_WallpaperCategory.WallPaper_Category_Item> categorys) {
        titleBar.setTitles(categorys);
        int len = categorys.size();
        for (int i = 0; i < len; i++) {
            addContentPage();
        }
    }

    private void addContentPage() {
        ScrollGridView gridView = new ScrollGridView(mContext);
        gridView.setStretchMode(GridView.NO_STRETCH);
        gridView.setVerticalSpacing(Div(42));
        gridView.setNumColumns(4);
        gridView.setHorizontalSpacing(Div(42 + 384));
        gridView.setSelector(android.R.color.transparent);
        gridView.setOverScrollMode(OVER_SCROLL_NEVER);
        gridView.setThumbV(R.drawable.gridview_scroll);
        gridView.setAnimation(R.anim.grid_anim);
        gridView.setFocusChangeLayout(false);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
        appPager.addView(gridView, params);
        WallpaperAdapter adapter = new WallpaperAdapter(mContext);
        gridView.setOnKeyListener(controller);
        gridView.setAdapter(adapter);
        gridView.showColumn(3, false);
        itemSelectedEvent.registerView(gridView, itemRevision, controller);
        gridView.setOnItemClickListener(controller);
        gridView.setFocusable(false);
        gridView.setFocusableInTouchMode(false);
    }


    public void showDivide(boolean show) {
        if (show)
            divide.setVisibility(View.VISIBLE);
        else
            divide.setVisibility(View.GONE);
    }

    public void showTips(SpannableString tips) {
        tipsView.setText(tips);
        if (tipsView.getVisibility() != View.VISIBLE)
            tipsView.setVisibility(View.VISIBLE);
    }

    public void hideTips() {
        if (tipsView.getVisibility() == View.VISIBLE)
            tipsView.setVisibility(View.GONE);
    }

    public void showTitleBar() {
        titleBar.showTitleBar();
    }

    public void setFocusVewAlpha(int alpha) {
        focusView.getBackground().setAlpha(alpha);
        LogUtils.i("pos", "alpha is " + alpha + " " + focusView.getVisibility());
    }

}
